package com.yauta.freegift.web;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.yauta.freegift.service.entity.UserDO;
import com.yauta.freegift.service.logic.IUserService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@org.springframework.stereotype.Controller
public class UserController implements Controller {

	@Autowired
	private IUserService userService;

	@Override
	@RequestMapping("/user")
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@RequestMapping("/user/add")
	public ModelAndView addUser(HttpServletRequest request, HttpServletResponse response) {
		String name = request.getParameter("name");
		String login = request.getParameter("login");
		String password = request.getParameter("password");
		if (name == null || name.isEmpty() || login == null || login.isEmpty() || password == null
				|| password.isEmpty()) {
			log.error("param error! name:" + name + ", login:" + login + ", password:" + password);
			return null;
		}
		long id = userService.addUser(new UserDO(0, name, login, password, "auto add"));
		log.info("new user id:" + id);
		return null;
	}

	@RequestMapping("/user/get")
	public ModelAndView getUser(HttpServletRequest request, HttpServletResponse response) {
		HashMap<Long, UserDO> map = userService.queryUser(0, 100);
		log.info(map.toString());
		return null;
	}
	
	@RequestMapping("/user/list")
	public ModelAndView listUser(HttpServletRequest request, HttpServletResponse response)
	{
		HashMap<Long, UserDO> map = userService.queryUser(0, 100);
		ModelAndView model = new ModelAndView();
		model.setViewName("user/list");
		model.addObject("users", map.values());
		return model;
	}

}
